using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Data;
using Eldorado.Object;
using Eldorado.Object.Query;
using NUnit.Framework;

using ProjectManagement.DataAccess;

namespace Northwind.NUnit
{
	[TestFixture]
	public class Test1 : BaseTest
	{
		public IObjectContext Context;

		[SetUp]
		public void Setup()
		{
			Context = CreateContext();
			Trace.Listeners.Add(new TextWriterTraceListener(Console.Out, "Console"));
		}

		[TearDown]
		public void TearDown()
		{
			Trace.Listeners.Remove("Console");
		}

		[Test]
		public void TestEmployee()
		{
			Employee result = CreateEmployee(Context);
			result.Name = "Name " + result.GetHashCode();
			Context.Update();
		}

		[Test]
		public void TestManager()
		{
			Manager result = CreateManager(Context);
			result.Name = "Name " + result.GetHashCode();
			result.Title = "Title " + result.GetHashCode();
			Context.Update();
		}

		[Test]
		public void TestProject()
		{
			Project result = CreateProject(Context);
			result.Name = "ProjectName " + result.GetHashCode();
			Context.Update();
		}

		[Test]
		public void TestQuery()
		{
			DataTable table = new DataTable();
			Context.RemoteSource.Fill(table, "Manager");
		}

		[Test]
		public void TestPolymorphicQuery()
		{
			DataTable table = new DataTable();
			Context.RemoteSource.Fill(table, "Employee");
		}

		[Test]
		public void TestPolymorphicCollect()
		{
			Employee employee = CreateEmployee(Context);
			employee.Name = "Employee " + employee.GetHashCode();

			Manager manager = CreateManager(Context);
			manager.Name = "Manager " + manager.GetHashCode();
			manager.Title = "Title " + manager.GetHashCode();

			Debug.Assert(Context.LocalStorage.GetCount("Employee") == 2);
			foreach(Employee e in Context.LocalStorage.Collect(typeof(Employee)))
			{
				Debug.Assert(Context.Domain.GetType(e) == typeof(Employee) || Context.Domain.GetType(e) == typeof(Manager));
			}
		}

		[Test]
		public void TestLocalCollect()
		{
			Employee employee = CreateEmployee(Context);
			employee.Name = "Employee " + employee.GetHashCode();

			Manager manager = CreateManager(Context);
			manager.Name = "Manager " + manager.GetHashCode();
			manager.Title = "Title " + manager.GetHashCode();

			Debug.Assert(Context.LocalStorage.GetCount("Employee e where e.Name like ?", "Manager%") == 1);
			foreach(Employee e in Context.LocalStorage.Collect("Employee e where e.Name like ?", "Manager%"))
			{
				Debug.Assert(Context.Domain.GetType(e) == typeof(Manager));
			}
		}

		[Test]
		public void TestJoin()
		{
			DataTable table = new DataTable();
			Context.RemoteSource.Fill(table, "select p.Name, m.Name, m.Title from Project p, p.Manager m");
		}
	}
}
